﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using Microsoft.Samples.Kinect.KinectExplorer;
using Microsoft.Samples.Kinect.WpfViewers;

namespace ZeroTouchUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly Uri iconUri = new Uri("pack://application:,,,/nsa.ico", UriKind.RelativeOrAbsolute);
        private readonly Uri expIconUri = new Uri("pack://application:,,,/KinectExplorer.ico", UriKind.RelativeOrAbsolute);
        public GestureRecognizer recognizer;
        public NotifyIcon trayIcon;
        public NotifyIcon recognizerIcon;
        bool trayCreated = false;

        System.Windows.Forms.ContextMenu trayMenu;

        public Microsoft.Samples.Kinect.KinectExplorer.KinectExplorer explorer;
        public Profile profile;

        public MainWindow()
        {
            InitializeComponent();
            
			profile = new Profile();
            ActivateExplorer();
            if (explorer != null && 
                explorer.sensorItems != null &&
                explorer.sensorItems.Count > 0 &&
                explorer.sensorItems[0].Sensor != null)
            {
                recognizer = new GestureRecognizer
                    { main = this, Kinect = explorer.sensorItems[0].Sensor,
                      Visibility = System.Windows.Visibility.Hidden,
                      WindowState = System.Windows.WindowState.Minimized,
                      profile = this.profile
                    };
                recognizer.Closing += new System.ComponentModel.CancelEventHandler(recognizer_Closing);
                recognizer.Show();
                recognizerIcon = new NotifyIcon();
                recognizerIcon.Text = "NSA Recognizer - Not Tracking";
                recognizerIcon.Icon = new Icon("KinectExplorer.ico");
                recognizerIcon.Click += new EventHandler(recognizerIcon_Click);
                recognizerIcon.Visible = true;
            }

            KinectSensor.KinectSensors.StatusChanged += this.KinectsStatusChanged;
        }

        void recognizer_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (recognizer != null)
            {
                e.Cancel = true;
                recognizer.Visibility = System.Windows.Visibility.Hidden;
                recognizer.WindowState = System.Windows.WindowState.Minimized;
            }
        }

        void recognizerIcon_Click(object sender, EventArgs e)
        {
            if (recognizer != null)
            {
                recognizer.Visibility = System.Windows.Visibility.Visible;
                recognizer.Show();
                recognizer.WindowState = System.Windows.WindowState.Normal;
                recognizer.Activate();
            }
        }

        private void KinectsStatusChanged(object sender, StatusChangedEventArgs e)
        {
            if (explorer != null &&
                explorer.sensorItems != null &&
                explorer.sensorItems.Count > 0 &&
                explorer.sensorItems[0].Sensor != null)
            {
                recognizer = new GestureRecognizer
                    { main = this, Kinect = explorer.sensorItems[0].Sensor,
                      Visibility = System.Windows.Visibility.Hidden,
                      WindowState = System.Windows.WindowState.Minimized,
                      profile = this.profile
                    };
                recognizer.Show();
                recognizer.Closing += new System.ComponentModel.CancelEventHandler(recognizer_Closing);
                if (recognizerIcon == null)
                {
                    recognizerIcon = new NotifyIcon();
                }
                recognizerIcon.Text = "NSA Recognizer - Not Tracking";
                recognizerIcon.Icon = new Icon("KinectExplorer.ico");
                recognizerIcon.Click += new EventHandler(recognizerIcon_Click);
                recognizerIcon.Visible = true;
            }
        }

        private void loadProfile_Click(object sender, RoutedEventArgs e)
        {
            /*if (explorer != null &&
                explorer.sensorItems != null &&
                explorer.sensorItems.Count > 0 &&
                explorer.sensorItems[0].Sensor != null)
            {*/
                Load load = new Load(profile, this);
                load.ShowDialog();
                Dollar.templates = profile.getDictionary();

                if (recognizer != null)
                {
                    recognizer.profile = profile;
                }
            /*}
            else
            {
                ActivateExplorer();
            }*/
        }

        private void editKeybinds_Click(object sender, RoutedEventArgs e)
        {
            /*if (explorer != null && 
                explorer.sensorItems != null &&
                explorer.sensorItems.Count > 0 &&
                explorer.sensorItems[0].Sensor != null)
            {*/
                Edit edit = new Edit(profile, this);
                edit.ShowDialog();
            /*}
            else
            {
                ActivateExplorer();
            }*/
        }

        private void exit_Click(object sender, RoutedEventArgs e)
        {
            foreach (Window w in System.Windows.Application.Current.Windows)
                w.Close();           

            //close the window
            this.Close();
        }   

        private void save_Click(object sender, RoutedEventArgs e)
        {
            if (this.profileName.Text != "")
            {
                Save save = new Save(profile);
                save.profileName.Text = this.profileName.Text;
                save.ShowDialog();
            }
            else
            {
                NamePrompt np = new NamePrompt();
                np.ShowDialog();
            }
        }

        private void ZeroTouch_Closed(object sender, EventArgs e)
        {
            foreach (Window w in System.Windows.Application.Current.Windows)
                w.Close();
            System.Windows.Application.Current.Shutdown();
        }

        private void ActivateExplorer()
        {
            if (explorer == null)
            {
                explorer = new Microsoft.Samples.Kinect.KinectExplorer.KinectExplorer { Icon = BitmapFrame.Create(expIconUri) };
                explorer.Closed += new EventHandler(Explorer_Closed);
                explorer.Show();
            }
            else
            {
                explorer.Activate();
                if (explorer.Visibility != System.Windows.Visibility.Visible)
                {
                    explorer.Show();
                }
            }
        }

        private void diagnosticViewer_Click(object sender, RoutedEventArgs e)
        {
            RemoveGestures rg = new RemoveGestures(this);
            rg.ShowDialog();
        }

        private void Explorer_Closed(object sender, EventArgs e)
        {
            foreach (KinectSensorItem kinect in explorer.sensorItems)
            {
                if (kinect.Window != null)
                {
                    kinect.Window.Close();
                }
            }

            explorer = null;
        }

        public void RefreshName()
        {
            profileName.Text = profile.Name;
        }

        //System Tray stuff
        private void toTray_Click(object sender, RoutedEventArgs e)
        {
            if (!trayCreated)
            {
                trayIcon = new NotifyIcon();
                trayMenu = new System.Windows.Forms.ContextMenu();
                trayMenu.MenuItems.Add("Bring up Console", Restore);
                trayMenu.MenuItems.Add("Exit", OnExit);
                trayIcon.Text = "NSA Console";
                trayIcon.Icon = new Icon("nsa.ico");
                trayIcon.ContextMenu = trayMenu;
                trayIcon.Click += new EventHandler(Restore);
            }
            trayIcon.Visible = true;
            this.Hide();
            if (this.explorer != null)
            {
                explorer.Hide();
            }
        }

        private void Restore(object sender, EventArgs e)
        {
            trayIcon.Visible = false;
            ActivateExplorer();
            this.Show();
            this.Width = 524;
            this.Height = 221;
        }

        private void OnExit(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}